package com.love.marriage.userActivityInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userActivityInfo.entity.UserActivityInfo;
import com.love.marriage.userActivityInfo.mapper.UserActivityInfoMapper;
import com.love.marriage.userActivityInfo.service.UserActivityInfoService;
import com.love.marriage.userActivityInfo.vo.UserActivityInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户活动信息表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-11-09 23:53:07
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserActivityInfoServiceImp extends ServiceImpl<UserActivityInfoMapper, UserActivityInfo> implements UserActivityInfoService {

    private final UserActivityInfoMapper userActivityInfoMapper;

    @Override
    public Page<UserActivityInfoVo> getPage(Long pageNum, Long pageSize, UserActivityInfoVo userActivityInfoVo) {
        Page<UserActivityInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userActivityInfoMapper.getPage(page, userActivityInfoVo);
    }

    @Override
    public UserActivityInfoVo queryUserActivityInfo(Long id) {
        return userActivityInfoMapper.queryUserActivityInfo(id);
    }

    @Override
    public UserActivityInfoVo queryUserActivityInfoByUser(Long userId) {
        return userActivityInfoMapper.queryUserActivityInfoByUser(userId);
    }


    @Override
    public UserActivityInfoVo addInfo(UserActivityInfoVo userActivityInfoVo) {
        UserActivityInfoVo dbInfo = queryUserActivityInfoByUser(userActivityInfoVo.getUserId());
        if(dbInfo != null) {
            userActivityInfoVo.setId(dbInfo.getId());
            return updateUserActivityInfo(userActivityInfoVo);
        } else {
            return addUserActivityInfo(userActivityInfoVo);
        }
    }


    @Override
    public UserActivityInfoVo addUserActivityInfo(UserActivityInfoVo userActivityInfoVo) {
        UserActivityInfo userActivityInfo = new UserActivityInfo();
        BeanUtils.copyProperties(userActivityInfoVo, userActivityInfo);
        userActivityInfoMapper.insert(userActivityInfo);
        UserActivityInfoVo res = new UserActivityInfoVo();
        BeanUtils.copyProperties(userActivityInfo, res);
        return res;
    }

    @Override
    public UserActivityInfoVo updateUserActivityInfo(UserActivityInfoVo userActivityInfoVo) {
        UserActivityInfo userActivityInfo = new UserActivityInfo();
        BeanUtils.copyProperties(userActivityInfoVo, userActivityInfo);
        userActivityInfoMapper.updateById(userActivityInfo);
        return userActivityInfoVo;
    }

    @Override
    public Boolean deleteUserActivityInfo(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userActivityInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
